package com.wf.course.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.tags.EscapeBodyTag;
import org.w3c.dom.ls.LSInput;

import com.utils.page.PageBean;
import com.wf.course.service.IIndexRecommendService;
import com.wf.course.dao.IIndexRecommendMapper;
import com.wf.course.model.ad.IndexRecommend;

@Service
@Transactional(rollbackFor=Exception.class)
public class IIndexRecommendServiceImpl implements IIndexRecommendService{
	
	@Autowired
	private IIndexRecommendMapper mapper;

	@Override
	public void indexInfo(PageBean<IndexRecommend> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(this.mapper.queryList(pageBean));
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean));
		
	}

	@Override
	public void saveOrUpdateIndexRecommend(IndexRecommend indexRecommend) {
		// TODO Auto-generated method stub
		
		//新增
		if (indexRecommend.getId() == null) {
	 
			this.mapper.saveIndexRecommend(indexRecommend);
		//修改
		} else {
		
			this.mapper.updateIndexRecommend(indexRecommend);
		}
		
	 
 }

	@Override
	public IndexRecommend queryIndexRecommendById(Integer id) {
		// TODO Auto-generated method stub
		return mapper.queryIndexRecommendById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
		 
	}

	@Override
	public boolean sorting(Integer indexTypeCode, Integer orderNum, Integer orderType,Integer id) {
		// TODO Auto-generated method stub
		boolean result = false;
		try {
			ArrayList<IndexRecommend> arrayList = null;
			//0上移
			if (orderType==0) {
				//获取需要排序的集合
				 arrayList = this.mapper.queryListByindexTypeCode(indexTypeCode, orderNum-1);
				//定义一个空的集合，放被点击排序的那条记录
				ArrayList<IndexRecommend> arrayListrecording = new ArrayList<IndexRecommend>();
				//定义开始的排序
				Integer startOrdeNum = orderNum-1 >0?orderNum-1:1;
				for (int i = 0; i < arrayList.size(); i++) {
					if (id.equals(arrayList.get(i).getId())) {
						arrayList.get(i).setOrderNum(startOrdeNum);
						arrayListrecording.add(arrayList.get(i));
						arrayList.remove(i);
					}
				}
				for (int i = 0; i < arrayList.size(); i++) {
					startOrdeNum++;
					arrayList.get(i).setOrderNum(startOrdeNum);
				}
				if (!arrayListrecording.isEmpty()) {
					arrayList.add(arrayListrecording.get(0));
				}
			}else {
				// 1或其他 下移
				//获取需要排序的集合
				 arrayList = this.mapper.queryListByindexTypeCode(indexTypeCode, orderNum);
				if (!arrayList.isEmpty() && arrayList.size()>1) {
					//定义一个空的集合，放被点击排序的那条记录
					//定义开始的排序
					Integer startOrdeNum = orderNum;
					ArrayList<IndexRecommend> arrayListrecording = new ArrayList<IndexRecommend>();
					for (int i = 0; i < arrayList.size(); i++) {
						if (id.equals(arrayList.get(i).getId())) {
							if (i<arrayList.size()-1) {
								arrayList.get(i+1).setOrderNum(orderNum);
								arrayListrecording.add(arrayList.get(i+1));
								arrayList.remove(i+1);
							}
						}
					}
					for (int i = 0; i < arrayList.size(); i++) {
						startOrdeNum++;
						arrayList.get(i).setOrderNum(startOrdeNum);
					}
					if (!arrayListrecording.isEmpty()) {
						arrayList.add(arrayListrecording.get(0));
					}
				}
			}
			mapper.updateOrderNumBatch(arrayList);
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
		}
		return result;
	}
}
